<template>
  <div class="editable-cell">
    <div v-if="editable" class="editable-cell-input-wrapper">
      <a-input-number :min="0" :value="value" @change="handleChange" @pressEnter="check" />
      <a-icon v-action:edit-price type="check" class="editable-cell-icon-check" @click="check" />
    </div>
    <div v-else class="editable-cell-text-wrapper">
      {{ value || ' ' }}
      <a-icon type="edit" v-action:edit-price class="editable-cell-icon" @click="edit" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'EditablePrice',
  props: {
    text: String,
  },
  data() {
    return {
      value: this.text,
      editable: false,
    }
  },
  methods: {
    handleChange(value) {
      this.value = value
    },
    check() {
      this.editable = false
      this.$emit('change', this.value)
    },
    edit() {
      this.editable = true
    },
  },
}
</script>

<style lang="less" scoped>
.editable-cell {
  .editable-cell-input-wrapper {
    .editable-cell-icon-check {
      margin-left: 4px;
      &:hover {
        color: #108ee9;
      }
    }
  }

  .editable-cell-text-wrapper {
    .editable-cell-icon {
      margin-left: 4px;
      &:hover {
        color: #108ee9;
      }
    }
  }
}
</style>